/**
 * Created by wuya on 16/1/18.
 */


$(document).ready(function () {
    var queryString = location.search;
    var parameters = Request.parseQueryString(queryString);

    var queryStartTime = null;
    var queryEndTime = null;

    if ( parameters.start && parameters.end ) {
        queryStartTime = new Date(Number(parameters.start));
        queryStartTime.setMinutes(queryStartTime.getMinutes() + 1);
        queryEndTime = new Date(Number(parameters.end));
    }

    var $startTime = $('.datepicker-start-time input').datepicker({
        timepicker:true,
        autoclose: false //这里最好设置为false
    });
    $startTime.datepicker('update', new Date());

    var $endTime = $('.datepicker-end-time input').datepicker({
        timepicker:true,
        autoclose: false, //这里最好设置为false
    });
    $endTime.datepicker('update', new Date());

    var $queryStartTime = $('#search_date_from').datepicker({
    });
    $queryStartTime.datepicker('update', new Date());
    if ( queryStartTime ) {
        $queryStartTime.datepicker('update', queryStartTime);
    }

    var $queryEndTime = $('#search_date_to').datepicker({
    });
    $queryEndTime.datepicker('update', new Date());
    if ( queryEndTime ) {
        $queryEndTime.datepicker('update', queryEndTime);
    }

    var $inputComment = $('.input-comment');
    var status = parameters.status;
    var $inputStatus = $("#search_status_input");
    if ( 'status' in parameters ) {
        $inputStatus.val(status);
        var statusName = $('.menu-status a[value="' + status + '"]').text();
        $inputStatus.parent('a').find('span').text(statusName);
    }

    $('.search_button').click(function(e) {
        disableAll(e);
    
        var startTime = $queryStartTime.datepicker('getDate');
        startTime = new Date(startTime.getFullYear(), startTime.getMonth(), startTime.getDate());
        var endTime = $queryEndTime.datepicker('getDate');
        endTime = new Date(endTime.getFullYear(), endTime.getMonth(), endTime.getDate());
        var status = $("#search_status_input").val();
    
        startTime.setMinutes(startTime.getMinutes() - 1);
        endTime.setDate(endTime.getDate() + 1);
        endTime.setMinutes(endTime.getMinutes() - 1);
        request = new Request('/student/attendance/search', {
            start: startTime.getTime(),
            end: endTime.getTime(),
            status: status
        });

        response = new Response();
        response.redirectTo(request);
    });

    $('.choose-term-list').click(function () {
        $('.choose-term-list').removeClass('active');
        $(this).addClass('active');
        $('.term-name').text($(this).text())
    })

    $('.btn-submit-apply').click(function() {
        var startTime = getDateOfDatePicker($startTime);
        var endTime = getDateOfDatePicker($endTime);

        if (startTime.getTime()>endTime.getTime()){
            alert('开始时间不能迟于结束时间');
            return;
        }

        var comment = $inputComment.val();

        console.log(startTime);
        console.log(endTime);
        console.log(comment);

        var request = new Request('/student/absence_apply/submit');
        request.postJSON({
            apply: {
                start: startTime,
                end: endTime,
                comment: comment
            }
        }, function(result) {
            console.log(result);

            if ( result.successful ) {
                alert('假条提交成功');
        
                request = new Request(location.href);
                response = new Response();
                response.redirectTo(request);
            }
            else {
                alert('假条提交失败');
            }
        });
    });

})

function tableDetailButtonClicked(event) {
    var detailButton = event.currentTarget;
    if (detailButton.innerHTML == '返回') {
        $(detailButton).removeClass('table_detail_background');
        $(detailButton).removeClass('table_detail_button_back');
        detailButton.innerHTML = '详细 <i class="iconfont icon-arrow-down attend_table_detail_arrow"></i>';
        var detail_content_id = 'attend_detail_content' + $(detailButton).attr('data-id');
        var detail_area_id = 'attend_detail_area' + $(detailButton).attr('data-id');
        $("[data-id="+detail_content_id+"]").animate({
            'height': '0',
            'padding': '0',
            'line-height': '0'
        }, 'fast', function () {
            $("[data-id="+detail_area_id+"]").addClass('table_detail_content_hide');
        });
    }
    else {
        $(detailButton).addClass('table_detail_background');
        $(detailButton).addClass('table_detail_button_back');
        detailButton.innerHTML = '返回';
        var detail_content_id = 'attend_detail_content' + $(detailButton).attr('data-id');
        var detail_area_id = 'attend_detail_area' + $(detailButton).attr('data-id');
        $("[data-id="+detail_area_id+"]").removeClass('table_detail_content_hide');
        $("[data-id="+detail_content_id+"]").animate({
            'height': 'auto',
            'padding': '6px 8px',
            'line-height': '18px'
        }, 'fast');
    }
}

//日期格式化为SUI日期
function formatDate(date) {
    var myDate = date;
    var month = myDate.getMonth() + 1;
    if (month < 10) {
        month = '0' + month;
    }
    var day = myDate.getDate();
    if (day < 10) {
        day = '0' + day;
    }
    return myDate.getFullYear() + '-' + month + '-' + day;
}

//获取当月的最后一天
function getTheLastDay(time) {
    var myTime = new Date();
    myTime.setFullYear(time.getFullYear());
    myTime.setMonth(time.getMonth() + 1);
    myTime.setDate(0);
    return myTime;
}

function disableFutureDay() {
    var today = new Date();
    var allMonths = $('.student_date_inline');
    for (var i=0; i<allMonths.length; i++) {
        var myMonth = allMonths[i];
        var allDays = $(myMonth).find('.day[class!="new disabled day"][class!="old disabled day"][class!="disabled day"]');
        for (var j=0; j<allDays.length; j++) {
            var myDay = allDays[j];
            var dayTime = new Date($(myMonth).attr('data-month') + '-' + ('00'+$(myDay).attr('data-day')).slice(-2));
            if (dayTime.getTime() > today.getTime()) {
                $(myDay).addClass('disabled');
            }
        }
    }
}
